#!/usr/bin/env bash
########################
choose_idea_ultimate_edition_or_community_edition() {
    if (whiptail --title "ultimate_edition_or_community_edition" --yes-button 'ultimate' --no-button 'community' --yesno "Do you want to choose ultimate edition or community edition?\nultimate为付费旗舰版本,community为免费社区版" 0 0); then
        GREP_NAME='intellij-idea-ultimate-edition'
        LNK_FILE='jetbrains-idea.desktop'
        BIN_FILE="/usr/bin/${GREP_NAME}"
        ICON_NAME="${GREP_NAME}.png"
        APP_OPT_DIR="/opt/${GREP_NAME}"
    else
        COMMUNITY_EDITION=true
        GREP_NAME='intellij-idea-community-edition'
        LNK_FILE='idea.desktop'
        BIN_FILE="/usr/bin/idea"
        ICON_NAME="idea.png"
        APP_OPT_DIR="/usr/share/idea"
    fi
}
########################
choose_pycharm_professional_edition_or_community_edition() {
    #已移除专业版，此函数无效
    if (whiptail --title "professional_edition_or_community_edition" --yes-button 'pro' --no-button 'community' --yesno "Do you want to choose professional edition or community edition?\nprofessional为付费专业版,community为免费社区版" 0 0); then
        GREP_NAME='pycharm-professional'
        LNK_FILE='charm.desktop pycharm-professional.desktop'
        BIN_FILE="/usr/bin/charm /usr/bin/pycharm"
        ICON_NAME="pycharm.png"
        APP_OPT_DIR="/opt/${GREP_NAME}"
    fi
}
development_programming_tools() {
    COMMUNITY_EDITION=false
    RETURN_TO_WHERE='development_programming_tools'
    GREP_NAME=''
    DEPENDENCY_02=""
    DEV_MENU=01
    SOFTWARE=$(
        whiptail --title "Development" --menu \
            "If you find the idea interface is a red screen,try using tightvnc server.\n若idea出现红屏,则请换用tightvnc服务" 0 50 0 \
            "1" "🇻 🇸 Visual Studio Code(现代化代码编辑器)" \
            "2" "🇦 🇸 Android Studio(Google推出的安卓IDE)" \
            "3" "🇮 🇯 IntelliJ IDEA(Java集成开发环境)" \
            "4" "🇵 🇨 PyCharm(Python集成开发环境)" \
            "5" "🇼 🇸 Webstorm(JavaScript IDE,Web前端开发工具)" \
            "6" "🇨 🇱 CLion(C/C++ IDE)" \
            "7" "🇬 🇴 GoLand(Golang IDE)" \
            "8" "GNU Emacs(可扩展,可定制,支持自文档化)" \
            "9" "Code::Blocks(C，C++和Fortran的IDE)" \
            "10" "github-desktop(x64,github官方桌面客户端)" \
            "11" "Sublime Text(x64,漂亮的UI,非凡的功能)" \
            "0" "🌚 Return to previous menu 返回上级菜单" \
            3>&1 1>&2 2>&3
    )
    #"12" "🇹 🇨 teamcity(持续集成Continue Integration工具)" \
    #"11" "⚛️ atom(x64,酷炫文本编辑器,github出品)" \
    #"8" "💎 RubyMine(Ruby IDE)" \
    #"9" "PhpStorm(Php IDE)" \
    case "${SOFTWARE}" in
    0 | "") software_center ;;
    1) source_vscode_script ;;
    2) #install_android_studio
        GREP_NAME='android-studio'
        LNK_FILE="${GREP_NAME}.desktop"
        BIN_FILE="/usr/bin/${GREP_NAME}"
        APP_OPT_DIR="/opt/${GREP_NAME}"
        DEV_MENU=02
        ;;
    3) choose_idea_ultimate_edition_or_community_edition ;;
    4)
        COMMUNITY_EDITION=true
        #choose_pycharm_professional_edition_or_community_edition
        GREP_NAME='pycharm-community-edition'
        LNK_FILE='pycharm.desktop'
        BIN_FILE="/usr/bin/pycharm"
        ICON_NAME="pycharm.png"
        APP_OPT_DIR="/usr/share/pycharm"
        ;;
    5)
        GREP_NAME='webstorm'
        LNK_FILE='jetbrains-webstorm.desktop'
        BIN_FILE="/usr/bin/${GREP_NAME}"
        ICON_NAME="${GREP_NAME}.svg"
        APP_OPT_DIR="/opt/${GREP_NAME}"
        ;;
    6)
        GREP_NAME='clion-1'
        LNK_FILE='jetbrains-clion.desktop'
        BIN_FILE="/usr/bin/clion"
        ICON_NAME="${GREP_NAME}.svg"
        APP_OPT_DIR="/opt/clion"
        ;;
    7)
        GREP_NAME='goland'
        LNK_FILE='jetbrains-goland.desktop'
        BIN_FILE="/usr/bin/${GREP_NAME}"
        ICON_NAME="${GREP_NAME}.png"
        APP_OPT_DIR="/opt/${GREP_NAME}"
        ;;
    8) install_emacs ;;
    9) install_code_blocks ;;
    10)
        GREP_NAME='github-desktop-bin'
        LNK_FILE="github-desktop.desktop"
        BIN_FILE="/usr/bin/github-desktop"
        ICON_NAME="github-desktop.png"
        APP_OPT_DIR="/opt/github-desktop"
        ;;
    999)
        GREP_NAME='atom-editor-bin'
        LNK_FILE='atom.desktop'
        BIN_FILE="/usr/bin/atom"
        ICON_NAME="atom.png"
        APP_OPT_DIR="/opt/${GREP_NAME}"
        ;;
    11) install_sublime_text_stable ;;
    esac
    #) printf "%s\n" "新版即将集成本开发工具的安装，敬请期待" ;;
    ############################################
    case ${GREP_NAME} in
    "") ;;
    *)
        case ${DEV_MENU} in
        01) dev_menu_01 ;;
        02) dev_menu_02 ;;
        esac
        ;;
    esac
    ####################
    press_enter_to_return
    development_programming_tools
}
################
install_emacs() {
    DEPENDENCY_01="emacs"
    beta_features_quick_install
}
#############
install_code_blocks() {
    DEPENDENCY_01="codeblocks"
    beta_features_quick_install
}
#############
dev_menu_02() {
    check_opt_app_version
    check_download_path
    RETURN_TO_WHERE='dev_menu_02'
    SOFTWARE=$(
        whiptail --title "${GREP_NAME}" --menu \
            "您想要对${GREP_NAME}小可爱做什么？" 0 50 0 \
            "1" "install/upgrade(安装/升级)" \
            "2" "del pkg(删除安装包)" \
            "3" "remove(卸载${GREP_NAME})" \
            "0" "🌚 Return to previous menu 返回上级菜单" \
            3>&1 1>&2 2>&3
    )
    case "${SOFTWARE}" in
    0 | "") development_programming_tools ;;
    1) install_ide_02 ;;
    2) delete_ide_pkg_02 ;;
    3) remove_ide_02 ;;
    esac
    ############################################
    press_enter_to_return
    dev_menu_02
}
###########
delete_ide_pkg_02() {
    cd ${DOWNLOAD_PATH}
    LOCAL_ARCH_PKG_VERSION=$(ls -t ${GREP_NAME}*.tar.gz 2>/dev/null | head -n 1)
    case ${LOCAL_ARCH_PKG_VERSION} in
    "") printf "%s\n" "检测到安装包${RED}不存在${RESET}" ;;
    *)
        printf "%s\n" "${RED}rm -v${RESET} ${BLUE}${LOCAL_ARCH_PKG_VERSION}${RESET}"
        ls -lh ${DOWNLOAD_PATH}/${LOCAL_ARCH_PKG_VERSION}
        printf "%s\n" "Do you want to delete it?"
        do_you_want_to_continue
        rm -v ${LOCAL_ARCH_PKG_VERSION}
        ;;
    esac
}
###########
dev_menu_01() {
    check_opt_app_version
    check_download_path
    RETURN_TO_WHERE='dev_menu_01'
    SOFTWARE=$(
        whiptail --title "${GREP_NAME}" --menu \
            "您想要对${GREP_NAME}小可爱做什么？" 0 50 0 \
            "1" "install/upgrade(安装/升级)" \
            "2" "del pkg(删除安装包)" \
            "3" "remove(卸载${GREP_NAME})" \
            "0" "🌚 Return to previous menu 返回上级菜单" \
            3>&1 1>&2 2>&3
    )
    case "${SOFTWARE}" in
    0 | "") development_programming_tools ;;
    1) install_ide_01 ;;
    2) delete_ide_pkg ;;
    3) remove_ide_01 ;;
    esac
    ############################################
    press_enter_to_return
    dev_menu_01
}
###########
delete_ide_pkg() {
    case ${LINUX_DISTRO} in
    debian)
        apt clean
        apt autoclean
        ;;
    esac
    cd ${DOWNLOAD_PATH}
    LOCAL_ARCH_PKG_VERSION=$(ls -t ${GREP_NAME}*.pkg.tar.zst 2>/dev/null | head -n 1)
    case ${LOCAL_ARCH_PKG_VERSION} in
    "") printf "%s\n" "检测到安装包${RED}不存在${RESET}" ;;
    *)
        printf "%s\n" "${RED}rm -v${RESET} ${BLUE}${LOCAL_ARCH_PKG_VERSION}${RESET}"
        ls -lh ${DOWNLOAD_PATH}/${LOCAL_ARCH_PKG_VERSION}
        printf "%s\n" "Do you want to delete it?"
        do_you_want_to_continue
        rm -v ${LOCAL_ARCH_PKG_VERSION}
        ;;
    esac
}
###########
remove_ide_01() {
    cd ${APPS_LNK_DIR}
    printf "%s\n" "${RED}rm -rv ${RESET} ${BLUE}${APP_OPT_DIR} ${LNK_FILE} /usr/share/pixmaps/${ICON_NAME} ${BIN_FILE} ${LOCAL_APP_VERSION_TXT}${RESET}"
    do_you_want_to_continue
    case ${GREP_NAME} in
    intellij-idea-community-*) rm -rv /usr/share/licenses/idea /usr/share/icons/hicolor/scalable/apps/idea.svg ;;
    pycharm-community-*) rm -rv /usr/share/licenses/pycharm /usr/share/icons/hicolor/scalable/apps/pycharm.svg ;;
    esac
    #删除两次
    case ${GREP_NAME} in
    github-desktop-bin)
        rm -rv /usr/share/lintian/overrides/github-desktop /usr/share/doc/github-desktop ${APP_OPT_DIR} ${LNK_FILE} ${BIN_FILE} ${LOCAL_APP_VERSION_TXT} /usr/share/icons/hicolor/256x256/apps/github-desktop.png /usr/share/icons/hicolor/512x512/apps/github-desktop.png /usr/share/icons/hicolor/1024x1024/apps/github-desktop.png
        ;;
    atom-editor-bin) rm -rv atom.desktop atom.desktop.orig /usr/bin/apm /usr/bin/atom /usr/share/atom /usr/share/doc/atom /usr/share/licenses/atom-editor-bin /usr/share/lintian/overrides/atom /usr/share/pixmaps/atom.png /usr/share/polkit-1/actions /usr/share/polkit-1/actions/atom-stable.policy ${LOCAL_APP_VERSION_TXT} ;;
    *) rm -rv ${APP_OPT_DIR} ${LNK_FILE} /usr/share/pixmaps/${ICON_NAME} ${BIN_FILE} ${LOCAL_APP_VERSION_TXT} ;;
    esac
}
############
remove_ide_02() {
    cd ${APPS_LNK_DIR}
    printf "%s\n" "${RED}rm -rv ${RESET} ${BLUE}${APP_OPT_DIR} ${LNK_FILE} ${LOCAL_APP_VERSION_TXT}${RESET}"
    do_you_want_to_continue
    case ${GREP_NAME} in
    *) rm -rv ${APP_OPT_DIR} ${LNK_FILE} ${LOCAL_APP_VERSION_TXT} ;;
    esac
}
#############
check_ide_version() {
    cat <<-ENDofTable
		╔═══╦══════════╦═══════════════════╦════════════════════
		║   ║          ║                   ║                    
		║   ║ software ║    ✨最新版本     ║   本地版本 🎪
		║   ║          ║  Latest version   ║  Local version     
		║---║----------║-------------------║--------------------
		║ 1 ║${GREP_NAME}                  
		║   ║          ║$(printf '%s\n' "${THE_LATEST_APP_VERSION_CODE}" | sed 's@-x86_64@@')
		║   ║          ║                    $(printf '%s\n' "${LOCAL_OPT_APP_VERSION}" | sed 's@-x86_64@@')

	ENDofTable
}
#########
uncompress_arch_pkg_tar_zst() {
    cd /
    tar -I zstd -Ppxvf ${DOWNLOAD_PATH}/${ARCH_WALLPAPER_VERSION} --exclude=.*
}
###########
if_you_want_to_install_manually() {
    printf "%s\n" "If you want to ${BLUE}install it ${YELLOW}manually${RESET},please go to the ${PURPLE}official website.${RESET}"
}
install_ide_01() {
    #check_opt_dir_01
    check_zstd
    case ${GREP_NAME} in
    webstorm) ICON_FILE='/opt/webstorm/bin/webstorm.png' ;;
    clion-1) ICON_FILE='/opt/clion/bin/clion.png' ;;
    github-desktop-bin) ICON_FILE='/usr/share/icons/hicolor/256x256/apps/github-desktop.png' ;;
    *) ICON_FILE="/usr/share/pixmaps/${ICON_NAME}" ;;
    esac
    cat_icon_img
    printf "%s\n" "正在检测版本更新信息..."
    case ${COMMUNITY_EDITION} in
    true) check_archlinux_community_html_date ;;
    *) check_archlinux_cn_html_date ;;
    esac
    ############
    THE_LATEST_APP_VERSION_CODE=$(cat index.html | grep '\.pkg\.tar\.zst' | egrep -v '\.xz\.sig|\.zst\.sig' | grep -v '\-jre\-' | grep "${GREP_NAME}" | tail -n 1 | cut -d '=' -f 3 | cut -d '"' -f 2 | sed "s@${GREP_NAME}-@@g" | sed 's@4%3A@@' | sed 's@.pkg.tar.zst@@')
    check_ide_version
    case ${GREP_NAME} in
    intellij-idea-*)
        #printf "%s\n" "本版本为${GREP_NAME}(旗舰版)，如需安装community edition(社区版)，则请前往官网手动下载安装。"
        if_you_want_to_install_manually
        printf "%s\n" "${YELLOW}https://www.jetbrains.com/idea/download/#section=linux${RESET}"
        ;;
    pycharm*)
        if_you_want_to_install_manually
        printf "%s\n" "${YELLOW}https://www.jetbrains.com/pycharm/download/#section=linux${RESET}"
        ;;
    goland*)
        if_you_want_to_install_manually
        printf "%s\n" "${YELLOW}https://www.jetbrains.com/goland/download/#section=linux${RESET}"
        ;;
    webstorm*)
        if_you_want_to_install_manually
        printf "%s\n" "${YELLOW}https://www.jetbrains.com/webstorm/download/#section=linux${RESET}"
        ;;
    clion*)
        if_you_want_to_install_manually
        printf "%s\n" "${YELLOW}https://www.jetbrains.com/clion/download/#section=linux${RESET}"
        ;;
    #pycharm-professional)
    #   printf "%s\n" "本版本为${GREP_NAME}(专业版)，如需安装community edition(社区版)，则请前往官网手动下载安装。"
    #   printf "%s\n" "${YELLOW}https://www.jetbrains.com/pycharm/download/#section=linux${RESET}"
    #  ;;
    android-studio)
        if_you_want_to_install_manually
        printf "%s\n" "${BLUE}https://developer.android.com/studio${RESET}"
        THE_LATEST_AS_LINK="$(curl -L 'https://developer.android.google.cn/studio/#downloads' | grep 'linux' | grep href | grep studio | tail -n 1 | cut -d '"' -f 2)"
        ;;
    github-desktop-bin)
        case ${ARCH_TYPE} in
        amd64) ;;
        *)
            printf "%s\n" "本版本仅支持x86_64架构"
            printf "%s\n" "${ARCH_TYPE}请${RED}自行编译${RESET}"
            printf "%s\n" "Github url: ${YELLOW}https://github.com/shiftkey/desktop${RESET}"
            arch_does_not_support
            ;;
        esac
        ;;
    atom-editor-bin)
        case ${ARCH_TYPE} in
        amd64) ;;
        *)
            printf "%s\n" "本版本仅支持x86_64架构"
            printf "%s\n" "${ARCH_TYPE}请${RED}自行编译${RESET}"
            printf "%s\n" "Github url: ${YELLOW}https://github.com/atom/atom${RESET}"
            arch_does_not_support
            ;;
        esac
        ;;
    esac
    do_you_want_to_upgrade_it
    #############
    grep_arch_linux_pkg_04
    uncompress_arch_pkg_tar_zst
    case ${GREP_NAME} in
    github-desktop-bin | atom-editor-bin) ;;
    *) install_java ;;
    esac
}
##################
install_ide_02() {
    ICON_FILE='/opt/android-studio/bin/studio.png'
    cat_icon_img
    printf "%s\n" "正在检测版本更新信息..."
    #check_archlinux_cn_html_date
    ############
    #THE_LATEST_APP_VERSION_CODE=$(cat index.html | grep '\.pkg\.tar\.zst' | egrep -v '\.xz\.sig|\.zst\.sig' | grep -v '\-jre\-' | grep "${GREP_NAME}" | tail -n 1 | cut -d '=' -f 3 | cut -d '"' -f 2 | sed "s@${GREP_NAME}-@@g" | sed 's@.pkg.tar.zst@@')
    case ${GREP_NAME} in
    android-studio)
        if_you_want_to_install_manually
        printf "%s\n" "${BLUE}https://developer.android.com/studio${RESET}"
        THE_LATEST_DEB_LINK="$(curl -L 'https://developer.android.google.cn/studio/#downloads' | grep 'linux' | grep href | grep studio | tail -n 1 | cut -d '"' -f 2)"
        THE_LATEST_APP_VERSION_CODE="$(printf '%s\n' "${THE_LATEST_DEB_LINK}" | awk -F '/' '{print $NF}' | sed 's@-linux.tar.gz@@' | sed 's@android-studio-ide-@@')"
        DOWNLOAD_FILE_NAME=$(printf '%s\n' "${THE_LATEST_DEB_LINK}" | awk -F '/' '{print $NF}')
        ;;
    esac
    check_ide_version
    do_you_want_to_upgrade_it
    #############
    #grep_arch_linux_pkg_04
    download_android_studio
    install_android_studio
    #uncompress_arch_pkg_tar_zst
    case ${GREP_NAME} in
    android-studio) install_java ;;
    esac
}
#############
creat_android_studio_application_link() {
    cd ${APPS_LNK_DIR}
    #Icon=android-studio
    cat >android-studio.desktop <<-'EOF'
		[Desktop Entry]
		Name=Android Studio
		Type=Application
		Comment=Android Studio provides the fastest tools for building apps on every type of Android device.
		Exec=/opt/android-studio/bin/studio.sh %F
		Icon=/opt/android-studio/bin/studio.svg
		Categories=TextEditor;Development;IDE;
		MimeType=text/plain;inode/directory;
		Terminal=false
		Actions=new-empty-window;
		StartupNotify=true
		StartupWMClass=Android-Studio
	EOF
    chmod a+r android-studio.desktop
}
#########################
download_android_studio() {
    cd ${DOWNLOAD_PATH}
    LOCAL_ARCH_PKG_VERSION=$(ls -t ${GREP_NAME}*.tar.gz 2>/dev/null | head -n 1)
    case ${LOCAL_ARCH_PKG_VERSION} in
    ${DOWNLOAD_FILE_NAME}) printf "%s\n" "检测到您已经下载最新版的${LOCAL_ARCH_PKG_VERSION},如需删除安装包，请输${RED}rm -v${RESET} ${BLUE}${DOWNLOAD_PATH}/${LOCAL_ARCH_PKG_VERSION}${RESET}" ;;
    *) aria2c --console-log-level=warn --no-conf --allow-overwrite=true -d ${DOWNLOAD_PATH} -o ${DOWNLOAD_FILE_NAME} -x 10 -s 10 -k 1M ${THE_LATEST_DEB_LINK} ;;
    esac
    printf "%s\n" "${THE_LATEST_APP_VERSION_CODE}" >"${LOCAL_APP_VERSION_TXT}"
}
###############
check_android_studio() {
    mkdir -pv ${HOME}/sd/Download
    cd ${HOME}/sd/Download
    if [ -e "/opt/android-studio" ]; then
        printf '%s\n' '您已安装Android studio'
        printf "%s\n" "若您需要卸载，则请输${RED}rm -rv${RESET} ${HOME}/sd/Download/android_studio_linux_64bit.tar.gz ${BLUE}/opt/android-studio ${APPS_LNK_DIR}/android_studio.desktop${RESET};${RED}${TMOE_REMOVAL_COMMAND}${RESET} ${BLUE}default-jre${RESET}"
        printf "%s\n" "是否需要重新安装？"
        printf "%s\n" "Do you want to reinstall it?"
        do_you_want_to_continue
    fi
    if [ ! -e "android_studio_linux_64bit.tar.gz" ]; then
        download_android_studio
    fi
    install_java
}
##############
install_android_studio() {
    #check_android_studio
    tar -pzxvf ${DOWNLOAD_PATH}/${DOWNLOAD_FILE_NAME} -C /opt
    creat_android_studio_application_link
    #printf "%s\n" "安装包位于${BLUE}${HOME}/sd/Download/android_studio_linux_64bit.tar.gz${RESET}"
    #printf "%s\n" "安装完成，如需卸载，则请输${RED}rm -rv${RESET} ${BLUE}/opt/android-studio ${APPS_LNK_DIR}/android_studio.desktop${RESET};${RED}${TMOE_REMOVAL_COMMAND}${RESET} ${BLUE}default-jre${RESET}"
}
##################
install_sublime_text_stable() {
    case ${ARCH_TYPE} in
    amd64 | i386) ;;
    *) arch_does_not_support ;;
    esac
    case ${LINUX_DISTRO} in
    debian)
        install_gpg
        GPG_URL='https://download.sublimetext.com/sublimehq-pub.gpg'
        GPG_FILE_NAME='sublimehq-pub.asc'
        cd /etc/apt/trusted.gpg.d
        if [ ! -e "${GPG_FILE_NAME}" ]; then
            printf "%s\n" "${YELLOW}${GPG_URL}${RESET}"
            aria2c --console-log-level=warn --no-conf --allow-overwrite=true -o ${GPG_FILE_NAME} ${GPG_URL}
            chmod a+r ${GPG_FILE_NAME}
        fi
        apt-key add ${GPG_FILE_NAME}
        printf "%s\n" "deb https://download.sublimetext.com/ apt/stable/" >/etc/apt/sources.list.d/sublime-text.list
        ;;
    arch)
        curl -O https://download.sublimetext.com/sublimehq-pub.gpg
        pacman-key --add sublimehq-pub.gpg
        pacman-key --lsign-key 8A8F901A
        rm sublimehq-pub.gpg
        if grep -q 'sublimetext' /etc/pacman.conf; then
            echo -e "\n[sublime-text]\nServer = https://download.sublimetext.com/arch/stable/x86_64" | sudo tee -a /etc/pacman.conf
        fi
        ;;
    redhat | suse)
        rpm -v --import https://download.sublimetext.com/sublimehq-rpm-pub.gpg
        if [ $(command -v dnf) ]; then
            dnf config-manager --add-repo https://download.sublimetext.com/rpm/stable/x86_64/sublime-text.repo
        elif [ $(command -v yum) ]; then
            yum-config-manager --add-repo https://download.sublimetext.com/rpm/stable/x86_64/sublime-text.repo
        elif [ $(command -v zypper) ]; then
            zypper addrepo -g -f https://download.sublimetext.com/rpm/stable/x86_64/sublime-text.repo
        fi
        ;;
    esac
    DEPENDENCY_01='sublime-text'
    DEPENDENCY_02=''
    beta_features_quick_install
}
##############
development_programming_tools
